import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'processQueryM',

  state: {
    tableData: { dataSource: [], columns: [], pageInfo: { tptal: 0, currentPage: 1, numInPage: 20 } },
    applyData: {},
    currentUser: {},
    selectData: {},
  },

  effects: {
    //查询
    *getTableData({ payload, callback }, { call, put }) {
      const res = yield call(api.processManage.getTableData, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData: res.data,
          },
        });
      } else {
        message.error(res?.msg || '列表查询失败');
      }
    },

    // 获取流程申请人
    *getApplyPeople({ payload, callback }, { call, put }) {
      const res = yield call(api.processManage.getApplyPeople, payload);
      if (res && res.success == true) {
        yield put({
          type: 'save',
          payload: {
            applyData: res.data,
          },
        });
      } else {
        message.error(res?.msg);
      }
    },
    //获取用户信息
    *getUserInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.user.queryCurrent, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: {
            currentUser: response.data,
          },
        });
        callback(response.data);
      }
    },

    //获取用户信息
    *getStationUserByUserId({ payload, callback }, { call, put }) {
      const response = yield call(api.processManage.getStationUserByUserId, payload);
      if (response && response?.code == 0) {
        callback(response.data?.dataSource || []);
      } else {
        callback([]);
      }
    },
    // 调单
    *turnOverOrder({ payload, callback }, { call, put }) {
      const response = yield call(api.processManage.turnOverOrder, payload);
      if (response && response?.code == 0) {
        callback('success');
        message.success(response?.msg || '调单成功');
      } else {
        callback('fail');
        message.error(response?.msg || '调单失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
